package com.wgj.android.web;
import android.app.usage.UsageEvents;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.just.agentweb.AgentWeb;
import com.wgj.android.R;
import com.wgj.android.fragment.CardFragment;
import com.wgj.android.fragment.TemplateFragment;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.web.entity.WebConfig;
import com.wgj.android.widget.imageselect.ImagePicker;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class XXXWebFragment extends Fragment {
    public  static  final  String WEB_CONFIG="web_config";
    private AgentWeb mAgentWeb;
    private LinearLayout webviewWarp;
    private  FragmentInterface androidInterface;
    private ImagePicker imagePicker;
    private String str;
    private  WebConfig config;
    public static XXXWebFragment getInstance(WebConfig config){
        XXXWebFragment webFragment=new XXXWebFragment();
        Bundle bundle=new Bundle();
        bundle.putSerializable(WEB_CONFIG,config);
        webFragment.setArguments(bundle);
        return  webFragment;
    }
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_teamwork, container, false);
        webviewWarp= view.findViewById(R.id.webviewWarp);
        return view;
    }
    @Override
    public void onActivityCreated( Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //url =getArguments().getString("url");
        config= (WebConfig) getArguments().getSerializable(WEB_CONFIG);
        initView();
    }
    @Override
    public void onStart() {
        super.onStart();
    }
    public  void initView(){
        mAgentWeb = AgentWeb.with(this)//传入Activity or Fragment
                .setAgentWebParent( webviewWarp, new LinearLayout.LayoutParams(-1, -1))
                .setIndicatorColorWithHeight(ContextCompat.getColor(getContext(),R.color.colorPrimary),1)// 使用默认进度条
                .createAgentWeb()
                .ready()
                .go(config.getUrl());
        mAgentWeb.getWebCreator().getWebView().setHorizontalScrollBarEnabled(false);
        mAgentWeb.getWebCreator().getWebView().setVerticalScrollBarEnabled(false);
        androidInterface=new FragmentInterface(mAgentWeb,this);
        mAgentWeb.getJsInterfaceHolder().addJavaObject("android",androidInterface);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==1000){
               mAgentWeb.getWebCreator().getWebView().loadUrl(WGJAPI.WEB_URL+"/live/index.html");
            return;
        }
        androidInterface.onActivityResult(requestCode,resultCode,data);
    }
    @Override
    public void onPause() {
        super.onPause();

    }
    @Override
    public void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        mAgentWeb.getWebLifeCycle().onDestroy();
        super.onDestroy();
    }
}
